package com.hyphenate.easeui.utils;

import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseUI;
import com.hyphenate.easeui.EaseUI.EaseUserProfileProvider;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.domain.EaseUser;

public class EaseUserUtils {

    static EaseUserProfileProvider userProvider;

    static {
        userProvider = EaseUI.getInstance().getUserProfileProvider();
    }

    /**
     * get EaseUser according username
     *
     * @param username hxName
     * @return
     */
    public static EaseUser getUserInfo(String username) {
        EaseUser user = null;
        if (userProvider != null)
            user = userProvider.getUser(username);
   /*     if (user == null || user.getNickname() == null || user.getAvatar() == null) {
            EMConversation conversation = EMClient.getInstance().chatManager().getConversation(username);
            //  Log.d("userUtils", "conversation--------------------->" + conversation);
            if (conversation != null) {
                EMMessage message = conversation.getLatestMessageFromOthers();
                if (message != null) {
                    user = new EaseUser(username);
                    //  user.set(message.getIntAttribute("userId", -1));
                    user.setAvatar(message.getStringAttribute("avatar", ""));
                    user.setNickname(message.getStringAttribute("nickName", ""));
                }
            }
        }*/
        // Log.d("userUtils", "user--------------------->" + user);
        return user;
    }

    /**
     * set user avatar
     *
     * @param username
     */
    public static void setUserAvatar(Context context, String username, ImageView imageView) {
        EaseUser user = getUserInfo(username);
        if (user != null && user.getAvatar() != null) {
            try {
                int avatarResId = Integer.parseInt(user.getAvatar());
                Glide.with(context)
                        .asBitmap()
                        .load(avatarResId).into(imageView);
            } catch (Exception e) {
                //use default avatar
                Glide.with(context)
                        .asBitmap()
                        .load(user.getAvatar())
                        .apply(RequestOptions.placeholderOf(R.drawable.em_common_head_place_holder)
                                .diskCacheStrategy(DiskCacheStrategy.ALL))
                        .into(imageView);
            }
        } else {
            Glide.with(context).load(R.drawable.em_common_head_place_holder).into(imageView);
        }
    }

    /**
     * set user's nickname
     */
    public static void setUserNick(String username, TextView textView) {
        if (textView != null) {
            EaseUser user = getUserInfo(username);
            if (user != null && user.getNickname() != null) {
                textView.setText(user.getNickname());
            } else {
                textView.setText(username);
            }
        }
    }

    public static void setGroupName(String username, TextView textView) {
        if (textView != null) {
            EaseUser user = getUserInfo(username);
            if (user != null && user.getNickname() != null) {
                textView.setText(user.getNickname());
                // textView.setText(String.format("#%s", user.getNickname()));
            } else {
                //textView.setText(String.format("#%s", username));
                textView.setText(username);
            }
        }
    }


}
